#pragma once
#include "libbase/common.h"

namespace wmj
{
    enum VaneLightType
    {
        VLT_ACTIVATABLE,    // 待激活
        VLT_ACTIVATED,      // 已激活
        VLT_UNKOWN          // 未知类型
    };

    // 二维叶片
    class Vane2f
    {
    public:
        Vane2f()
        :   m_type(VLT_UNKOWN),
            m_angle(0),
            m_time(-1),
            m_point2f(cv::Point2f(-1, -1)),
            m_center2f(cv::Point2f(-1, -1))
        {}
        Vane2f(cv::Point2f point, float big_contour_rect_ratio, cv::RotatedRect big_rect, cv::RotatedRect small_rect, cv::Point2f base_point)
        :   Vane2f()
        {
            m_point2f = point;
            m_big_rect = big_rect;
            m_small_rect = small_rect;
            // 判断是否激活
            if (big_contour_rect_ratio > 0.25 && big_contour_rect_ratio < 0.5)
            {
                m_type = VLT_ACTIVATABLE;
            }
            else if (big_contour_rect_ratio > 0.7 && big_contour_rect_ratio < 1.0)
            {
                // 仅用于判断是否只剩最后一个叶片的逻辑中，阈值可以给得比较宽松
                m_type = VLT_ACTIVATED;
            }
            else
            {
                m_type = VLT_UNKOWN;
            }

            // 整理装甲板灯条矩形角点顺序
            m_vertices.clear();
            cv::Point2f img_points[4];
            cv::RotatedRect light_rect = m_small_rect;
            // 短边减半以将装甲板矩形缩小为灯条对矩形
            if (light_rect.size.width > light_rect.size.height)
            {
                light_rect.size.height /= 2;
            }
            else
            {
                light_rect.size.width /= 2;
            }
            light_rect.points(img_points);
            for (int i = 0; i < 4; i++)
            {
                m_vertices.push_back(img_points[i] + base_point);
            }
            // 01应为短边
            if (sqrt(pow(m_vertices[0].x - m_vertices[1].x, 2) + pow(m_vertices[0].y - m_vertices[1].y, 2)) >
                sqrt(pow(m_vertices[0].x - m_vertices[3].x, 2) + pow(m_vertices[0].y - m_vertices[3].y, 2)))
            {
                std::swap(m_vertices[1], m_vertices[3]);
            }
            // 01应在左侧
            if (m_vertices[1].x > m_vertices[3].x)
            {
                std::swap(m_vertices[0], m_vertices[3]);
                std::swap(m_vertices[1], m_vertices[2]);
            }
            // 交换01，交换23，因为monocular里面有诡异操作
            std::swap(m_vertices[0], m_vertices[1]);
            std::swap(m_vertices[2], m_vertices[3]);
        }

        void print(std::string tag)
        {
            std::string prefix = "[Rune2f] " + tag + " ";
            std::cout << prefix << "time: " << m_time << std::endl;
            std::cout << prefix << "angle: " << m_angle << std::endl;
            std::cout << prefix << "point: " << m_point2f << std::endl;
            std::cout << prefix << "center: " << m_center2f << std::endl;
        }

        VaneLightType m_type;           // 叶片类型
        double m_angle;                 // 叶片角度，范围[-PI, Pi]，以 x 轴正方向为零点，逆时针为正向
        double m_time;                  // 叶片被拍摄的时间，单位秒，以相机时间轴为准
        cv::Point2f m_point2f;          // 目标坐标
        cv::Point2f m_center2f;         // 中心点坐标
        cv::Point2f m_fake_center;      // fake center
        cv::RotatedRect m_big_rect;             // 叶片矩形，坐标在ROI图上
        cv::RotatedRect m_small_rect;           // 装甲板矩形，坐标在ROI图上
        std::vector<cv::Point2f> m_vertices;    // 单目测距所用灯条角点
    };

    // 三维叶片
    class Vane3f
    {
    public:
        Vane3f()
        :   m_type(VLT_UNKOWN),
            m_angle(0),
            m_time(-1),
            m_point3f(cv::Point3f(-1, -1, -1)),
            m_center3f(cv::Point3f(-1, -1, -1))
        {}

        void print(std::string tag)
        {
            std::string prefix = "[Rune3f] " + tag + " ";
            std::cout << prefix << "time: " << m_time << std::endl;
            std::cout << prefix << "angle: " << m_angle << std::endl;
            std::cout << prefix << "point: " << m_point3f << std::endl;
            std::cout << prefix << "center: " << m_center3f << std::endl;
        }

        VaneLightType m_type;           // 叶片类型
        double m_angle;                 // 叶片角度，范围[-PI, Pi]，以 x 轴正方向为零点，逆时针为正向
        double m_time;                  // 叶片被拍摄的时间，单位秒，以相机时间轴为准
        cv::Point3f m_point3f;          // 三维坐标
        cv::Point3f m_center3f;         // 中心点坐标
    };
    
    // 能量机关状态
    // enum ROBO_ENERGY
    // {
    //     ENERGY_IDLE  = 0x00,
    //     ENERGY_SMALL = 0x01,
    //     ENERGY_BIG   = 0x02,
    // };
}